package cn.mcch.investigator.presenter;

import android.content.Context;
import android.view.View;

import java.util.Map;

import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.BaseModel;
import cn.mcch.investigator.utils.L;
import cn.mcch.investigator.viewdelegate.BaseViewDelegate;

/**
 * presenter基类
 *
 * @param <V> BaseViewDelegate
 * @param <M> model
 * @author Cao Mingming
 * @date 2016-5-4 16:24:27
 */
public abstract class BasePresenter<V extends BaseViewDelegate, M extends BaseModel> {

    /**
     * view代理
     */
    protected V mViewDelegate;
    /**
     * model
     */
    protected M mModel;

    public BasePresenter() {
        try {
            mViewDelegate = getViewDelegateClass().newInstance();
            if (getModelClass() != null) {
                mModel = getModelClass().newInstance();
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public abstract Class<V> getViewDelegateClass();

    public abstract Class<M> getModelClass();

    public View getRootView() {
        return mViewDelegate.getRootView();

    }

    public void createRootView(Context context) {
        if (mViewDelegate != null) {
            mViewDelegate.createRootView(context);
        }
    }

    public void initTitleBar(Context context, boolean isTranslucentStatus, String title, int ivLeftResourceId, int ivRightResourceId, int bgColorResourceId,
                             OnCommonClickListener l) {
        if (mViewDelegate != null) {
            mViewDelegate.initTitleBar(isTranslucentStatus, context, title, ivLeftResourceId, ivRightResourceId, bgColorResourceId, l);
        }
    }

    public void initConetentView(Context context) {
        if (mViewDelegate != null) {
            mViewDelegate.initConetentView(context);
        }
    }

    public void createFirstView(Context context, int layoutId, OnCommonClickListener l) {
        if (mViewDelegate != null) {
            mViewDelegate.createFirstView(context, layoutId, l);
        }
    }

    /**
     * 初始化加载数据的时候使用
     *
     * @param service
     * @param params
     * @param context
     * @param l
     * @param successViewLayoutId
     */
    public void initData(String service, Map<String, String> params, final Context context, final OnCommonClickListener l,
                         final int successViewLayoutId) {
        if (mModel != null) {
            mModel.request(60000, service, params, new OnResponseListener() {

                @Override
                public void onBefore(int id) {

                }

                @Override
                public void onAfter(boolean responseResult, int id) {
                    if (!responseResult) {
                        mViewDelegate.createLoadErrorView(context, l);
                    }

                }

                @Override
                public void onSuccess(String json, int id) {

                    mViewDelegate.createSuccessView(context, successViewLayoutId, l);
                    // view绑定数据
                    if (mViewDelegate != null) {
                        viewBindData(mViewDelegate, json);
                    }
                }
            });
        }
    }

    /**
     * 界面初始化需要加载数据时调用，需要时重写
     */
    public void viewBindData(V viewDelegate, String json) {

    }


    /**
     * 界面关闭时调用，销毁model 和 viewDelegate
     */
    public void clear() {
        if (mViewDelegate != null) {
            mViewDelegate=null;
        }
        if (mModel != null) {
            mModel.cancelTag();
            mModel = null;
        }
    }

}
